function getAttributes(el)
{
	var atts = el.attributes;
	var realAtts = {};
	
	for(var i=0; i< atts.length; i++)
	realAtts[atts[i].nodeName]=atts[i].nodeValue;
	
	return realAtts;
}

Plex.Lib.JS.load("Data.Jax");
Plex.Lib.JS.load("UI.Widget.Fly_Window");
Plex.Lib.JS.load("UI.Widget.Fly_Option");

Plex.Util.Product_Translator = function (product_ID)
{
	var self = this;
	this.window = new Plex.UI.Widget.Fly_Window({title:'Product Translation Editor'});
	this.Jax = new Plex.Data.Jax();
	this.currentLanguage = false;
	this.initialized = false;
	this.product_ID=product_ID
	this.currentData = false;
	
	this.el = {};
	
	this.el.table = new Element('table',{
		'cellspacing':0,
		'cellpadding':0,
		'border':0,
		'styles': {
			'width':'800px',
			'height':'400px'
		}
	})
		
	this.el.mainRow = new Element('tr').injectInside(self.el.table);
	
	this.el.leftCol = new Element('td',{
		'valign':'top',
		'styles':{
			'border-color':'#707070 rgb(0, 0, 0) rgb(0, 0, 0) rgb(112, 112, 112)',
			'border-style':'solid',
			'border-width':'1px',
			'background-color':'#414141',
			'height':'400px',
			'width':'15%'
		}
	}).injectInside(self.el.mainRow);
	
	this.el.rightCol = new Element('td',{
		'valign':'top',
		'styles':{
			'border-color':'#707070 rgb(0, 0, 0) rgb(0, 0, 0) rgb(112, 112, 112)',
			'border-style':'solid',
			'border-width':'1px',
			'background-color':'#414141',
			'height':'400px',
			'width':'85%'
		}
	}).injectInside(self.el.mainRow);
	
	this.el.editor = new Element('div',{
		'styles':{
			'opacity':'0',
			'margin-left':'5%',
			'width':'90%'
		}
	}).injectInside(self.el.rightCol);
	
	this.el.editor.fx = new Fx.Styles(this.el.editor, {duration: 250,wait: false,transition: Fx.Transitions.Quad.easeOut});
	
	this.el.langTitle = new Element('h3',{
		'styles':{
			'margin-top':10,
			'color':'#eeeeee'
		}
	}).injectInside(self.el.editor);
		
	this.el.nameTitle = new Element('h3',{
		'styles':{
			'margin-top':20,
			'margin-bottom':5,
			'color':'#cccccc'
		}
	}).setText("Product Name").injectInside(self.el.editor);
			
	this.el.nameValue = new Element('input',{
		'class':'post',
		'styles':{
			'width':'100%',
		},
		'events':{
			'keyup':function()
			{
				self.checkForChanges();
			}
		}
	}).injectInside(self.el.editor);
			
	this.el.descriptionTitle = new Element('h3',{
		'styles':{
			'margin-top':25,
			'margin-bottom':5,
			'color':'#cccccc'
		}
	}).setText("Product Description").injectInside(self.el.editor);
			
	this.el.descriptionValue = new Element('textarea',{
		'class':'post',
		'styles':{
			'width':'100%',
			'height':'200px',
		},
		'events':{
			'keyup':function()
			{
				self.checkForChanges();
			}
		}
	}).injectInside(self.el.editor);
			
	this.el.buttons = new Element('div',{
		'styles':{
			'margin-top':15,
			'float':'right'
		}
	}).injectInside(self.el.editor);
	
	
	this.el.revertButton = new Element('input',{
		'type':'button',
		'class':'button',
		'value':'Revert Changes',
		'styles':{
			'color':'#AAAAAA',
			'margin-right':'8px'
		},
		'events':{
			'click': function(){
				self.el.nameValue.value=self.currentData.product_name;
				self.el.descriptionValue.value=self.currentData.product_description;
				self.checkForChanges();
			}
		}
	}).injectInside(this.el.buttons)
	
	
	this.el.submitAllButton = new Element('input',{
		'type':'button',
		'class':'button',
		'value':'Save Changes To All',
		'styles':{
			'color':'#AAAAAA',
			'margin-right':'8px'
		},
		'events':{
			'click': function()
			{
				var optionBox = new Plex.UI.Widget.Fly_Option({
				title:'Are You Sure?',
				options: 
					[
						{
							'text':'Yes, Save This To All The Languages',
							'onclick':function(){
								self.updateAllLanguages();
								this.close();
							}
						},
						{
							'text':'No, Just Save This For The Current Language',
							'onclick':function(){
								self.updateLanguage();
								this.close();
							}
						},
						{
							'text':"No, Don't Save To Anything",
							'onclick':function()
							{
								this.close();
							}
						}
					]
				});
				
			  	optionBox.open();
			}
		}
	}).injectInside(this.el.buttons)
	
	this.el.submitButton = new Element('input',{
		'type':'button',
		'class':'button',
		'value':'Save Changes',
		'styles':{
			'color':'#AAAAAA'
		},
		'events':{
			'click': function(){
				self.updateLanguage();
			}
		}
	}).injectInside(this.el.buttons)
	
	this.el.leftTable = new Element('table',{
		'width':'100%'
	}).injectInside(self.el.leftCol);
	
	this.checkForChanges = function()
	{
		if(self.el.nameValue.value!=self.currentData.product_name || self.el.descriptionValue.value!=self.currentData.product_description)
		{
			self.el.revertButton.disabled=false;
			self.el.submitAllButton.disabled=false;
			self.el.submitButton.disabled=false;
			self.el.revertButton.set({'styles':{'color':'#FFFFFF'}})
			self.el.submitAllButton.set({'styles':{'color':'#FFFFFF'}})
			self.el.submitButton.set({'styles':{'color':'#FFFFFF'}})
		}
		else
		{
			self.el.revertButton.disabled=true;
			self.el.submitAllButton.disabled=true;
			self.el.submitButton.disabled=true;
			self.el.revertButton.set({'styles':{'color':'#AAAAAA'}})
			self.el.submitAllButton.set({'styles':{'color':'#AAAAAA'}})
			self.el.submitButton.set({'styles':{'color':'#AAAAAA'}})
		}

	}
	
	var langButton = function(info)
	{
		var button = {el:{}};
		
		button.info = info;
		button.el.row = new Element('tr',{})
		
		button.el.col = new Element('td',{
			'styles': {
				'border':'2px solid transparent',
				'cursor':'default',
				'font-weight':'900',
				'color':'White'
			},
			'events':{				
				'mouseover': function(e)
				{
					this.style.border='2px groove grey'
					this.style.backgroundColor='#555555';
				},
				'mouseout': function(e)
				{
					this.style.border='2px solid transparent'
					this.style.backgroundColor='';
				},
				'click':function(e)
				{
					self.loadLanguage(button)
				}
			}	
		}).setText(info.language).injectInside(button.el.row);
		
		button.moveTo = function(par)
		{
			button.el.row.injectInside(par);
		}
		
		return button;
	}
	
	this.updateLanguage = function()
	{
		var QJax = new Plex.Data.Jax('Product','Language_Update');
		QJax.Var('language_ID',self.currentLanguage.info.language_ID);
		QJax.Var('product_ID',self.product_ID);
		QJax.Var('product_name',self.el.nameValue.value);
		QJax.Var('product_description',self.el.descriptionValue.value);
		
		QJax.handler = function(resp)
		{
			self.currentData.product_name = self.el.nameValue.value;
			self.currentData.product_description = self.el.descriptionValue.value;
			self.checkForChanges();
		}
		
		QJax.Call();
	}
	
	this.updateAllLanguages = function()
	{
		var QJax = new Plex.Data.Jax('Product','Multi_Language_Update');
		QJax.Var('product_ID',self.product_ID);
		QJax.Var('product_name',self.el.nameValue.value);
		QJax.Var('product_description',self.el.descriptionValue.value);
		
		QJax.handler = function(resp)
		{
			self.currentData.product_name = self.el.nameValue.value;
			self.currentData.product_description = self.el.descriptionValue.value;
			self.checkForChanges();
		}
		
		QJax.Call();
	}
	
	this.loadLanguage = function(lang)
	{
		self.currentLanguage = lang;
		
		self.el.editor.fx.start({'opacity':0}).chain(function()
		{
			self.Jax.feed = 'Product';
			self.Jax.action = 'Search';
			self.Jax.Var('language_ID',self.currentLanguage.info.language_ID);
			self.Jax.Var('product_ID',self.product_ID);
			
			self.Jax.handler = function(resp)
			{
				self.el.langTitle.setText(lang.info.language);
				
				var product = getAttributes(resp.getElementsByTagName('results').item(0).childNodes.item(0))
				
				self.el.nameValue.value = product.product_name;
				self.el.descriptionValue.value = product.product_description;
							
				self.el.editor.fx.start({
					'opacity':1,
				})
				
				self.currentData = product;
			}
			
			self.Jax.Call();
		})
	}
	
	this.loadLanguages = function()
	{
		self.Jax.feed = 'Language';
		self.Jax.action = 'Search';
		self.Jax.Var('row_status_ID',1)
		
		self.Jax.handler = function(resp)
		{
			
			var results = resp.getElementsByTagName('results').item(0).childNodes;
			
			Plex.S$.empty(self.el.leftTable);
			
			for(var i=0; i<results.length; i++)
			{
				var result = getAttributes(results[i]);
				var button = new langButton(result);
				button.moveTo(self.el.leftTable);
			}
			
		}
		
		self.Jax.Call();
	}
		
	this.open = function()
	{
		if(!self.initialized)
		self.init();
		
		self.window.open(800,400);
	}
	
	this.close = function()
	{
		self.window.close();
	}
	
	this.init = function()
	{
		self.el.table.injectInside(self.window.body);
		self.loadLanguages();
		
		self.initialized = true;
	}
}